package com.petalsgroup.petals.web.servlet.comments;

import com.petalsgroup.petals.pojo.*;
import com.petalsgroup.petals.service.animation.AnimationService;
import com.petalsgroup.petals.service.animation.CommentService;
import com.petalsgroup.petals.service.animation.impl.AnimationServiceImpl;
import com.petalsgroup.petals.service.animation.impl.CommentServiceImpl;
import com.petalsgroup.petals.service.user.impl.UserServiceImpl;
import com.petalsgroup.petals.util.LimitUtil;
import com.petalsgroup.petals.util.RecentlyUtil;
import com.petalsgroup.petals.web.BaseServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.*;

@WebServlet("/comments/*")
public class CommentServlet extends BaseServlet {
    private final CommentService commentService=new CommentServiceImpl();
    public void showAllComments(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从首页缩略图点击传到servlet转发的动漫aid数据
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //获取Session域中user对象
        UserDO user = (UserDO) request.getSession().getAttribute("user");
        List<CommentsDO> commentsList=commentService.getAllComments(aid);
        CommentsDO myComment=commentService.getMyComment(aid,user.getUid());
        List<String> userNameList=commentService.getComment(commentsList);
        String myUserName=commentService.getMyUserName(user.getUid());
        HashMap<String,Object> map = new HashMap<>();
        map.put("commentsList", commentsList);
        map.put("userNameList",userNameList);
        map.put("myUserName",myUserName);
        ResultInfo info = new ResultInfo();
        info.setData(map);
        writeValue(response,info);
    }
    public void addComment(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //给相应用户添加评论
        Map<String, String[]> map = request.getParameterMap();
        Timestamp timestamp = new Timestamp(new java.util.Date().getTime());
        /*for (String s: map.keySet()) {
            System.out.println(s+","+map.get(s)[0]);
        }*/
        commentService.addComment(Integer.parseInt(map.get("aid")[0]),Integer.parseInt(map.get("uid")[0]),map.get("comment_content")[0],timestamp,Integer.parseInt(map.get("score")[0]));
        response.sendRedirect(request.getContextPath()+"/animation/show_detailed?aid="+Integer.parseInt(map.get("aid")[0]));
    }
    public void limitComment(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //传入参数
        String currentPageStr=request.getParameter("currentPage");
        String numStr = request.getParameter("num");
        String aidStr = request.getParameter("aid");
        UserDO user = (UserDO) request.getSession().getAttribute("user");
        //System.out.println(currentPageStr+","+numStr+","+aidStr+","+user.getUid());
        //如果不传递页码，则默认第一页
        int currentPage,num;
        //处理获得的参数
        if(currentPageStr!=null&&currentPageStr.length()>0){
            currentPage=Integer.parseInt(currentPageStr);
        }else {
            currentPage=1;
        }
        //如果不传递，则默认每页五条
        if(numStr!=null&&numStr.length()>0){
            num=Integer.parseInt(numStr);
        }else {
            num=2;
        }
        //调用service查询PageBean对象
        PageBean<CommentsVO> pb = commentService.pageQuery(Integer.parseInt(aidStr),currentPage,num,user.getUid());
        //创建List集合存储用户名
        List<Integer> userIdList=new ArrayList<>();
        for (CommentsVO commentsVO:pb.getList()) {
            userIdList.add(commentsVO.getCommentsDO().getUid());
        }
        PageBean<String> userNameList=commentService.userNamePageQuery(Integer.parseInt(aidStr),currentPage,num,userIdList);
        ResultInfo resultInfo=new ResultInfo();
        HashMap<String,Object> map=new HashMap<>();
        map.put("pb",pb);
        map.put("userNameList",userNameList);
        map.put("userIdList",userIdList);
        resultInfo.setData(map);
        //转换为json对象
        writeValue(response,resultInfo);
    }
    public void limitTimeComment(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //传入参数
        String currentPageStr=request.getParameter("currentPage");
        String numStr = request.getParameter("num");
        String aidStr = request.getParameter("aid");
        UserDO user = (UserDO) request.getSession().getAttribute("user");
        //System.out.println(currentPageStr+","+numStr+","+aidStr+","+user.getUid());
        //如果不传递页码，则默认第一页
        int currentPage,num;
        //处理获得的参数
        if(currentPageStr!=null&&currentPageStr.length()>0){
            currentPage=Integer.parseInt(currentPageStr);
        }else {
            currentPage=1;
        }
        //如果不传递，则默认每页五条
        if(numStr!=null&&numStr.length()>0){
            num=Integer.parseInt(numStr);
        }else {
            num=2;
        }
        //调用service查询PageBean对象
        PageBean<CommentsVO> pb = commentService.pageTimeQuery(Integer.parseInt(aidStr),currentPage,num,user.getUid());
        //创建List集合存储用户名
        List<Integer> userIdList=new ArrayList<>();
        for (CommentsVO commentsVO:pb.getList()) {
            userIdList.add(commentsVO.getCommentsDO().getUid());
        }
        PageBean<String> userNameList=commentService.userNamePageQuery(Integer.parseInt(aidStr),currentPage,num,userIdList);
        ResultInfo resultInfo=new ResultInfo();
        HashMap<String,Object> map=new HashMap<>();
        map.put("pbTime",pb);
        map.put("userNameTimeList",userNameList);
        resultInfo.setData(map);
        //转换为json对象
        writeValue(response,resultInfo);
    }

    public void tagsComments(HttpServletRequest request,HttpServletResponse response) throws IOException {
        Integer uid= Integer.valueOf(request.getParameter("uid"));
        Integer com_id=Integer.valueOf(request.getParameter("com_id"));
        //System.out.println("uid:"+request.getParameter("uid"));
        //System.out.println("com_id:"+request.getParameter("com_id"));
        boolean flag=commentService.getLikeComment(uid,com_id);
        //System.out.println("flag:"+flag);
        if(!flag){
            commentService.removeLikeComment(uid,com_id);
        }else commentService.addLikeComment(uid,com_id);
        writeValue(response,flag);
    }
}
